package com.jieshen;

import com.jieshen.component.BackGroundPanel;
import com.jieshen.util.PathUtils;
import com.jieshen.util.ScreenUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class RegisterDemo {

        JFrame jf = new JFrame("注册");

        final int WIDTH = 500;
        final int HEIGHT = 400;

        //组装视图
        public void init() throws Exception {
            Point point = ScreenUtils.setCenter(500, 400);
            int x = point.x;
            int y = point.y;
            //设置窗口的属性
            jf.setBounds(x,y,WIDTH,HEIGHT);
            jf.setResizable(false);
            jf.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("logo.png"))));

            BackGroundPanel bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("regist.jpg"))));
            bgPanel.setBounds(0,0,WIDTH,HEIGHT);


            Box vBox = Box.createVerticalBox();

            //组装用户名
            Box uBox = Box.createHorizontalBox();
            JLabel uLabel = new JLabel("用户名：");
            JTextField uField = new JTextField(15);

            uBox.add(uLabel);
            uBox.add(Box.createHorizontalStrut(20));
            uBox.add(uField);

            //组装密码
            Box pBox = Box.createHorizontalBox();
            JLabel pLabel = new JLabel("密    码：");
            JTextField pField = new JTextField(15);

            pBox.add(pLabel);
            pBox.add(Box.createHorizontalStrut(20));
            pBox.add(pField);

            //组装手机号
            Box tBox = Box.createHorizontalBox();
            JLabel tLabel = new JLabel("手机号：");
            JTextField tField = new JTextField(15);

            tBox.add(tLabel);
            tBox.add(Box.createHorizontalStrut(20));
            tBox.add(tField);

            //组装性别
            Box gBox = Box.createHorizontalBox();
            JLabel gLabel = new JLabel("性    别：");
            JRadioButton maleBtn = new JRadioButton("男",true);
            JRadioButton femaleBtn = new JRadioButton("女",false);

            //实现单选的效果
            ButtonGroup bg = new ButtonGroup();
            bg.add(maleBtn);
            bg.add(femaleBtn);

            gBox.add(gLabel);
            gBox.add(Box.createHorizontalStrut(20));
            gBox.add(maleBtn);
            gBox.add(femaleBtn);
            gBox.add(Box.createHorizontalStrut(120));

            //组装验证码
            Box cBox = Box.createHorizontalBox();
            JLabel cLabel = new JLabel("验证码：");
            JTextField cField = new JTextField(4);

            cBox.add(cLabel);
            cBox.add(Box.createHorizontalStrut(20));
            cBox.add(cField);

            //组装按钮
            Box btnBox = Box.createHorizontalBox();
            JButton registBtn = new JButton("注册");
            JButton backBtn = new JButton("返回登录页面");


            backBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //返回到登录页面即可
                    try {
                        new Login().init();
                        jf.dispose();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });


            btnBox.add(registBtn);
            btnBox.add(Box.createHorizontalStrut(80));
            btnBox.add(backBtn);

            vBox.add(Box.createVerticalStrut(50));
            vBox.add(uBox);
            vBox.add(Box.createVerticalStrut(20));
            vBox.add(pBox);
            vBox.add(Box.createVerticalStrut(20));
            vBox.add(tBox);
            vBox.add(Box.createVerticalStrut(20));
            vBox.add(gBox);
            vBox.add(Box.createVerticalStrut(20));
            vBox.add(cBox);
            vBox.add(Box.createVerticalStrut(20));
            vBox.add(btnBox);

            bgPanel.add(vBox);

            jf.add(bgPanel);

            jf.setVisible(true);

            jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }

    public static void main(String[] args) throws Exception {
        new RegisterDemo().init();
    }
}
